package com.wolf.cx330.utils.act

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.donkingliang.imageselector.utils.ImageSelector
import com.wolf.cx330.R
import com.wolf.cx330.base.BaseActivity
import com.wolf.cx330.databinding.ActImgSelectBinding

class ImgSelectAct : BaseActivity() {

    val dataList = mutableListOf<String>()

    var imgAdapter: ImgAdapter? = null

    companion object {
        fun actionStart(context: Context) {
            context.startActivity(Intent(context, ImgSelectAct::class.java))
        }
    }

    private val REQUEST_CODE = 10001
    lateinit var binding: ActImgSelectBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActImgSelectBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ImageSelector.preload(this)
        checkPermission {
            imgAdapter = ImgAdapter()
            binding.resultRecyclerView.layoutManager = GridLayoutManager(this, 5, GridLayoutManager.VERTICAL, false)
            binding.resultRecyclerView.adapter = imgAdapter
            binding.selectImgBtn.setOnClickListener {
                ImageSelector.builder()
                    //  设置是否使用拍照
                    .useCamera(false)
                    //  设置是否单选
                    .setSingle(false)
                    //  图片的最大选择数量，小于等于0时，不限数量。
                    .setMaxSelectCount(9)
                    //  把已选的图片传入默认选中。
                    .setSelected(arrayListOf())
                    //  是否可以预览图片，默认为true
                    .canPreview(true)
                    //  打开相册
                    .start(this, REQUEST_CODE)
            }
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        data?.let {
            if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
                val list = data.getStringArrayListExtra(ImageSelector.SELECT_RESULT) as ArrayList<String>
                dataList.addAll(list)
                imgAdapter?.notifyDataSetChanged()

            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        //  清空缓存
        ImageSelector.clearCache(this)
    }


    inner class ImgAdapter : RecyclerView.Adapter<ImgViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImgViewHolder =
            ImgViewHolder(LayoutInflater.from(this@ImgSelectAct).inflate(R.layout.item_img_select, parent, false))

        override fun onBindViewHolder(holder: ImgViewHolder, pos: Int) {
            holder.imageView.setImageBitmap(BitmapFactory.decodeFile(dataList[pos]))
        }

        override fun getItemCount(): Int = dataList.size
    }

    inner class ImgViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val imageView: ImageView = itemView.findViewById(R.id.imageView)
    }
}